const path = require('path');
const webpack = require('webpack');
const buildPath = path.resolve(__dirname, '../public');
const nodeModulesPath = path.resolve(__dirname, '../node_modules');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: path.join(__dirname, '../lib/index.js'),
  devtool: 'source-map',
  output: {
    path: buildPath,
    filename: 'app.js',
  },
  plugins: [
    // 定义生产环境构建时允许React去除不必要的检查
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    // 汇编之间保持哈希一致
    new webpack.optimize.OccurrenceOrderPlugin(),
    // 压缩
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
      },
    }),
    new webpack.NoEmitOnErrorsPlugin(),
    new ExtractTextPlugin('styles/main.min.css')
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel-loader'],
        exclude: [nodeModulesPath],
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                url: false,
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]'
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                plugins: [
                  require('autoprefixer')({
                    browsers: [
                      'last 2 versions',
                      'IE >= 9',
                      'safari >= 8',
                      'Firefox >= 20'
                    ]
                  }),
                ]
              }
            },
            {
              loader: 'sass-loader',
              options: {
                outputStyle: 'compressed'
              }
            }
          ],
        })
      }
    ],
  },
};